package com.mcsyr.clearitem;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {
    public static Main plugin;
    public static FileConfiguration Config;
    public static Boolean DustbinLock = false;
    public static int DustbinCount = 0;

    public static Boolean PublicDustbinEnable;
    public static String PublicDustbinName;
    public static Integer PublicDustbinPageSize;
    public static String PublicDustbinPrevButtonName;
    public static String PublicDustbinPrevButtonType;
    public static String PublicDustbinNextButtonName;
    public static String PublicDustbinNextButtonType;
    public static Integer PublicDustbinClearInterval;
    public static List<String> PublicDustbinWhiteList;
    public static List<String> PublicDustbinWhiteListName;
    public static String PublicDustbinMessageReminder;
    public static String PublicDustbinMessageClear;

    public static Boolean PrivateDustbinEnable;
    public static String PrivateDustbinName;
    public static Integer PrivateDustbinSize;
    public static List<String> PrivateDustbinWhiteListName;
    public static List<String> PrivateDustbinWhiteListLore;
    public static String PrivateDustbinMessageClear;

    public static Boolean DropEnable;
    public static Integer DropTime;
    public static String DropMessageOpen;
    public static String DropMessageClose;
    public static String DropMessageDiscardInOpen;

    public static Integer ClearItemTime;
    public static List<String> ClearItemWhiteList;
    public static List<String> ClearItemWhiteListName;
    public static List<String> ClearItemWhiteListLore;
    public static Integer ClearItemChunkMaxItems;
    public static Boolean ClearItemItemFrame;
    public static Boolean ClearItemBoat;
    public static Boolean ClearItemExpBall;
    public static Boolean ClearItemFallingBlock;
    public static Boolean ClearItemPainting;
    public static Boolean ClearItemMinecart;
    public static Boolean ClearItemArrow;
    public static Boolean ClearItemSnowball;
    public static String ClearItemMessageClearPre;
    public static String ClearItemMessageClearStart;
    public static String ClearItemMessageClear;
    public static String ClearItemMessageClearWorld;
    public static String ClearItemMessageClearChunkMaxItems;

    public static Boolean CleaningTipsEnable;


    public static Integer time = 0;
    public static Integer WasteTotal = 0;
    public static Integer DustbinClearFrequency = 0;
    public static Map<Player, Boolean> PlayerDropLock = new HashMap<>();
    public static Map<Player, Date> PlayerDropLockTime = new HashMap<>();
    public static Map<Player, Inventory> PlayerPrivateDustbin = new HashMap<>();

    public void onEnable() {
        (Main.plugin = this).saveDefaultConfig();
        Bukkit.getServer().getConsoleSender().sendMessage("§b[ClearItem] 异步清理垃圾 插件启动");
        Bukkit.getServer().getConsoleSender().sendMessage("§b[ClearItem] By QQ2032704270");
        Main.loadConfig();
        Dustbin.init(); //  初始化公共垃圾箱
        tools.TraversePlayer(); // 遍历在线玩家初始化数据
        tools.Scheduler();  // 定时清理任务
        Bukkit.getServer().getPluginCommand("clearitem").setExecutor(new command());
        Bukkit.getServer().getPluginManager().registerEvents(new Event(), this);
    }

    public static void loadConfig() {
        Main.plugin.reloadConfig();
        Main.Config = Main.plugin.getConfig();

        Main.PublicDustbinEnable = Main.Config.getBoolean("PublicDustbin.Enable");
        Main.PublicDustbinName = Main.Config.getString("PublicDustbin.Name").replaceAll("&", "§");
        Main.PublicDustbinPageSize = Main.Config.getInt("PublicDustbin.PageSize");
        Main.PublicDustbinPrevButtonName = Main.Config.getString("PublicDustbin.PrevButton.Name").replaceAll("&", "§");
        Main.PublicDustbinPrevButtonType = Main.Config.getString("PublicDustbin.PrevButton.Type");
        Main.PublicDustbinNextButtonName = Main.Config.getString("PublicDustbin.NextButton.Name").replaceAll("&", "§");
        Main.PublicDustbinNextButtonType = Main.Config.getString("PublicDustbin.NextButton.Type");
        Main.PublicDustbinClearInterval = Main.Config.getInt("PublicDustbin.ClearInterval");
        Main.PublicDustbinWhiteList = Main.Config.getStringList("PublicDustbin.WhiteList");
        Main.PublicDustbinWhiteListName = Main.Config.getStringList("PublicDustbin.WhiteListName");
        Main.PublicDustbinMessageReminder = Main.Config.getString("PublicDustbin.Message.Reminder").replaceAll("&", "§");
        Main.PublicDustbinMessageClear = Main.Config.getString("PublicDustbin.Message.Clear").replaceAll("&", "§");

        Main.PrivateDustbinEnable = Main.Config.getBoolean("PrivateDustbin.Enable");
        Main.PrivateDustbinName = Main.Config.getString("PrivateDustbin.Name").replaceAll("&", "§");
        Main.PrivateDustbinSize = Main.Config.getInt("PrivateDustbin.Size");
        Main.PrivateDustbinWhiteListName = Main.Config.getStringList("PrivateDustbin.WhiteListName");
        Main.PrivateDustbinWhiteListLore = Main.Config.getStringList("PrivateDustbin.WhiteListLore");
        Main.PrivateDustbinMessageClear = Main.Config.getString("PrivateDustbin.Message.Clear").replaceAll("&", "§");

        Main.DropEnable = Main.Config.getBoolean("Drop.Enable");
        Main.DropTime = Main.Config.getInt("Drop.Time") * 1000;
        Main.DropMessageOpen = Main.Config.getString("Drop.Message.Open").replaceAll("&", "§");
        Main.DropMessageClose = Main.Config.getString("Drop.Message.Close").replaceAll("&", "§");
        Main.DropMessageDiscardInOpen = Main.Config.getString("Drop.Message.DiscardInOpen").replaceAll("&", "§");

        Main.ClearItemTime = Main.Config.getInt("ClearItem.Time");
        Main.ClearItemChunkMaxItems = Main.Config.getInt("ClearItem.ChunkMaxItems");
        Main.ClearItemWhiteList = Main.Config.getStringList("ClearItem.WhiteList");
        Main.ClearItemWhiteListName = Main.Config.getStringList("ClearItem.WhiteListName");
        Main.ClearItemWhiteListLore = Main.Config.getStringList("ClearItem.WhiteListLore");
        Main.ClearItemItemFrame = Main.Config.getBoolean("ClearItem.ItemFrame");
        Main.ClearItemBoat = Main.Config.getBoolean("ClearItem.Boat");
        Main.ClearItemExpBall = Main.Config.getBoolean("ClearItem.ExpBall");
        Main.ClearItemFallingBlock = Main.Config.getBoolean("ClearItem.FallingBlock");
        Main.ClearItemPainting = Main.Config.getBoolean("ClearItem.Painting");
        Main.ClearItemMinecart = Main.Config.getBoolean("ClearItem.Minecart");
        Main.ClearItemArrow = Main.Config.getBoolean("ClearItem.Arrow");
        Main.ClearItemSnowball = Main.Config.getBoolean("ClearItem.Snowball");
        Main.ClearItemMessageClearPre = Main.Config.getString("ClearItem.Message.ClearPre").replaceAll("&", "§");
        Main.ClearItemMessageClearStart = Main.Config.getString("ClearItem.Message.ClearStart").replaceAll("&", "§");
        Main.ClearItemMessageClear = Main.Config.getString("ClearItem.Message.Clear").replaceAll("&", "§");
        Main.ClearItemMessageClearWorld = Main.Config.getString("ClearItem.Message.ClearWorld").replaceAll("&", "§");
        Main.ClearItemMessageClearChunkMaxItems = Main.Config.getString("ClearItem.Message.ClearChunkMaxItems").replaceAll("&", "§");

        Main.CleaningTipsEnable = Main.Config.getBoolean("CleaningTips.Enable");
        Dustbin.setDustbinButton();
    }
}